package br.ufpe.cin.dsoa.handler.dependency;

import java.util.ArrayList;
import java.util.List;

import org.apache.felix.ipojo.metadata.Element;

import br.ufpe.cin.dsoa.common.Constants;
import br.ufpe.cin.dsoa.common.Expression;
import br.ufpe.cin.dsoa.common.Slo;

public class DependencyMetadata {

	private List<Slo> slos;
	private Class specification;

	public DependencyMetadata(Class spec, Element serviceTag) {   
		this.slos = new ArrayList<Slo>();
		// Initializes field
		this.specification = spec;

		// SLOs
		buildSLOs(serviceTag);
	}

	private void buildSLOs(Element demand){
		Element[] sloSet = demand.getElements(Constants.SLO_ELEMENT);
		for (Element sloEle : sloSet){

			//name
			String attribute = sloEle.getAttribute(Constants.SLO_ATTRIBUTE_ATTRIBUTE);

			//value
			double value = Double.parseDouble(sloEle.getAttribute(Constants.SLO_VALUE_ATTRIBUTE));

			//expression
			String expression = sloEle.getAttribute(Constants.SLO_EXPRESSION_ATTRIBUTE);

			//target
			String operation = sloEle.getAttribute(Constants.SLO_OPERATION_ATTRIBUTE);

			// statistic
			String statistic = sloEle.getAttribute(Constants.SLO_STATISTIC_ATTRIBUTE);

			//weight
			long weight = Long.parseLong(sloEle.getAttribute(Constants.SLO_WEIGHT_ATTRIBUTE));

			//window.value
			double windowValue = Double.parseDouble(sloEle.getAttribute(Constants.SLO_WINDOW_VALUE));

			//window.unit
			String windowUnit = sloEle.getAttribute(Constants.SLO_WINDOW_UNIT);

			Slo  slo = new Slo(attribute, Expression.valueOf(expression), value, operation, statistic, weight, windowValue, windowUnit);

			getSlos().add(slo);
		}
	}

	public List<Slo> getSlos() {
		return slos;
	}

	public void setSlos(List<Slo> slos) {
		this.slos = slos;
	}

	public Class getSpecification() {
		return specification;
	}
	
}
